package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.BuildingListBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.BuildingAddPest;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.CommentView;
import com.sx.zongzhi.view.IPublicBeanView;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 楼宇/修改新增
 */
public class BuildingAddActivity extends BaseActivity implements IPublicBeanView,CommentView {

    @BindView(R.id.et_BuildinglName)
    EditText et_BuildinglName;
    @BindView(R.id.sp_Type)
    Spinner sp_Type;
    @BindView(R.id.sp_Structure)
    Spinner sp_Structure;
    @BindView(R.id.et_Location)
    EditText et_Location;
    @BindView(R.id.et_Owner)
    EditText et_Owner;
    @BindView(R.id.et_Person)
    EditText et_Person;
    @BindView(R.id.et_Phone)
    EditText et_Phone;
    private BuildingListBean.RowsBean detailBean;
    private String orgId="";
    private boolean isAdd=false;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_building_add);
        orgId=getIntent().getStringExtra("orgId");
        detailBean= (BuildingListBean.RowsBean) getIntent().
                getSerializableExtra("BuildingListBean.RowsBean");
        setTitleRight(R.string.save);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {

        PublicListPest  publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("出租房结构");//建筑结构
        publicListPest.getPublicList("楼宇类型");//楼宇类型
        if (detailBean==null){
            setTitleName("楼宇新增");
            isAdd=true;
        }else {
            isAdd=false;
            setTitleName("楼宇编辑");
            showView();
        }
    }

    private void showView() {
        et_BuildinglName.setText(detailBean.getBuildingname());
        et_Location.setText(detailBean.getBuildingaddress());
        et_Owner.setText(detailBean.getOwner());
        et_Person.setText(detailBean.getResponsibleperson());
        et_Phone.setText(detailBean.getPhone());
    }


    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
        }
    }



    /**
     * 提交
     */
    private void submit() {
        String name=et_BuildinglName.getText().toString().trim();
        if (BaseUtil.isEmpty(name)){
            et_BuildinglName.setError("名字不能为空");
            return;
        }
        if (detailBean==null){
            detailBean=new BuildingListBean.RowsBean();
        }
        detailBean.setBuildingname(et_BuildinglName.getText().toString().trim());
        detailBean.setBuildingaddress(et_Location.getText().toString().trim());
        detailBean.setOwner(et_Owner.getText().toString().trim());
        detailBean.setResponsibleperson(et_Person.getText().toString().trim());
        detailBean.setPhone(et_Phone.getText().toString().trim());
        detailBean.setBuildingstructures((GlobalBean) sp_Structure.getSelectedItem());
        detailBean.setType((GlobalBean) sp_Type.getSelectedItem());
        BuildingAddPest addPest=new BuildingAddPest(this);
        LoadDialog.show(this,"正在申请...");
        if (isAdd){
            addPest.addBuilding(orgId,"",detailBean,"addBuilddatas.action");
        }else {
            addPest.addBuilding(orgId,detailBean.getId(),detailBean,"updateBuilddatas.action");
        }
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("出租房结构".equals(publicBean.getDomainName())){
            String id = "";
            if (detailBean != null && detailBean.getBuildingstructures() != null) {
                id = detailBean.getBuildingstructures().getId();
            }
            showSpinner(sp_Structure, publicBean.getDicts(),id);
        }
        if ("楼宇类型".equals(publicBean.getDomainName())){
            String id = "";
            if (detailBean != null && detailBean.getType() != null) {
                id = detailBean.getType().getId();
            }
            showSpinner(sp_Type, publicBean.getDicts(),id);
        }
    }

    private void showSpinner(Spinner spinner, List<GlobalBean> datas, String id) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,datas);
        spinner.setAdapter(adapter);
        if (!BaseUtil.isEmpty(id)){
            int count=datas.size();
            for (int i=0;i<count;i++){
                if (id.equals(datas.get(i).getId())){
                    spinner.setSelection(i);
                    break;
                }
            }
        }

    }

    @Override
    public void commentSuccess(Boolean result) {
        LoadDialog.dismiss(this);
        if (result){
            MyRxBus.get().post(new MsgEvent<>("BuildingActivity",1,"操作成功"));
            ToastUtils.showToast(this,"操作成功");
            finish();
        }else {
            ToastUtils.showToast(this,"编辑失败");
        }
    }
}
